package mo.daliy;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class L3186 {

    public long maximumTotalDamage(int[] power) {
        TreeMap<Integer, Integer> powerCnt = new TreeMap<>();
        for (int ele : power) {
            powerCnt.put(ele, powerCnt.getOrDefault(ele, 0)+1);
        }
        int n = powerCnt.size();
        long[] dp = new long[n];
        long[] spell = new long[n];
        long[] counts = new long[n];
        int i = 0;
        for (Map.Entry<Integer, Integer> entry : powerCnt.entrySet()) {
            spell[i] = entry.getKey();
            counts[i] = entry.getValue();
            i++;
        }

        for (int j = 0; j < spell.length; j++) {
            dp[j] = spell[j] * counts[j];
            if (j >= 1) {
                dp[j] = Math.max(dp[j], dp[j-1]);
            }
            if (j >=1 && spell[j] - spell[j-1] > 2) {
                dp[j] = Math.max(dp[j], dp[j-1]+spell[j]*counts[j]);
            } else if (j >= 2 && spell[j] - spell[j-2] > 2) {
                dp[j] = Math.max(dp[j], dp[j-2]+spell[j]*counts[j]);
            } else if (j >= 3) {
                dp[j] = Math.max(dp[j], dp[j-3]+spell[j]*counts[j]);
            }
        }
        return dp[n-1];
    }


    public static void main(String[] args) {
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        treeMap.put(1, 1);
        treeMap.put(2, 2);
        treeMap.put(3, 3);
        Set<Map.Entry<Integer, Integer>> entries = treeMap.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            System.out.println(entry.getKey()+"_"+entry.getValue());
        }

        Set<Integer> set = new HashSet<>();
        set.iterator();
    }
}
